﻿<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Bigdata extends Controller
{
	private $_template_f;
	private $_lang;
	private $_fix_username = '-tuanha,-ngocnd,-toannh,';
	private $_fix_user_id = '-298,-311,-204,';
	private $_numItem = 30;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->_template_f = $this->config->item('template_f');
		
		$this->load->model('bigdata/bigdata_model');
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	function index()
	{		
		if(!$this->session->userdata('uname'))
		{
			//echo 'unlogin';
			redirect(site_url('login/?url='.site_url('bigdata')));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				$this->load->language('common', $this->_lang);
				$common_lang = $this->lang->line('common_lang');
				show_error($common_lang['access_denied']);
				die();
			}
			else
			{
				$uid = '-' . trim($this->session->userdata('uid')) . ',';
				$uname = '-' . trim($this->session->userdata('uname')) . ',';
				
				if(strrpos($this->_fix_username, $uname, 0) === false || strrpos($this->_fix_user_id, $uid,0) === false)
				{
					redirect(site_url());
					die();
				}
			}
		}
		
		// load header
		$this->_loadHeader('Bigdata Monitor');
		
		// load menu
		$this->_loadMenu();
		
		// load content
		$this->load->view($this->_template_f . 'bigdata/index_view');
		
		// load footer
		$this->_loadFooter();
	}
	
	function optpriority()
	{
		if(!$this->session->userdata('uname'))
		{
			//echo 'unlogin';
			redirect(site_url('login/?url='.site_url('bigdata/optpriority')));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				$this->load->language('common', $this->_lang);
				$common_lang = $this->lang->line('common_lang');
				show_error($common_lang['access_denied']);
				die();
			}
			else
			{
				$uid = '-' . trim($this->session->userdata('uid')) . ',';
				$uname = '-' . trim($this->session->userdata('uname')) . ',';
				
				if(strrpos($this->_fix_username, $uname, 0) === false || strrpos($this->_fix_user_id, $uid,0) === false)
				{
					redirect(site_url());
					die();
				}
			}
		}
		
		// load header
		$this->_loadHeader('Bigdata Monitor');
		
		// load menu
		$this->_loadMenu();
		
		// load content
		$data = array();
		$data['lstUser'] = $this->bigdata_model->optpriority_get_all();
		
		$data['lstGroup'] =  $this->bigdata_model->get_all_group();
		$this->load->view($this->_template_f . 'bigdata/optpriority_view', $data);
		
		// load footer
		$this->_loadFooter();
	}
	
	function optpriorityaction()
	{
		$data = array();
		$data['r'] = '1';
		$data['mess'] = '';
		if(!$this->session->userdata('uname'))
		{
			$data['r'] = '0';
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				$data['r'] = '2';
			}
			else
			{
				$uid = '-' . trim($this->session->userdata('uid')) . ',';
				$uname = '-' . trim($this->session->userdata('uname')) . ',';
				
				if(strrpos($this->_fix_username, $uname, 0) === false || strrpos($this->_fix_user_id, $uid,0) === false)
				{
					$data['r'] = '2';
				}
			}
		}
		
		if($data['r'] == '1')
		{
			$action = strtolower(trim($this->input->get('act')));
			$action = ($action == 'add' || $action == 'del') ? $action : '';
			if($action != '')
			{
				if($action == 'add')
				{
					$val = floatval($this->input->get('val'));
					$val = ($val < 0) ? 0 : $val;
					$val = ($val > 4) ? 4 : $val;
					$id = (int) $this->input->get('uid');
					if($id > 0)
					{
						$this->bigdata_model->optpriority_action('0', $id, $val);
					}
				}
				else if($action == 'del')
				{
					$id = (int) $this->input->get('uid');
					if($id > 0)
					{
						$this->bigdata_model->optpriority_action('1', $id, $val);
					}
				}
			}
			$data['mess'] = 'ok';
		}
		
		echo json_encode($data);
	}
	
	function updateoptpriority()
	{
		$data = array();
		$data['r'] = '1';
		$data['mess'] = '';
		if(!$this->session->userdata('uname'))
		{
			$data['r'] = '0';
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				$data['r'] = '2';
			}
			else
			{
				$uid = '-' . trim($this->session->userdata('uid')) . ',';
				$uname = '-' . trim($this->session->userdata('uname')) . ',';
				
				if(strrpos($this->_fix_username, $uname, 0) === false || strrpos($this->_fix_user_id, $uid,0) === false)
				{
					$data['r'] = '2';
				}
			}
		}
		
		if($data['r'] == '1')
		{
			$val = (int) $this->input->get('val');
			$val = $val/10;
			$id = (int) $this->input->get('uid');
			$this->bigdata_model->optpriority_action('2', $id, $val);
			$data['mess'] = 'ok';
		}
		
		echo json_encode($data);
	}
	
	function search_user()
	{
		$right = '1';
		if(!$this->session->userdata('uname'))
		{
			$right = '0';
			die('loign');
		}
		else
		{
			$permitId = $this->session->userdata('permitid');
			if(!is_numeric($permitId) || $permitId > -1)
			{
				$right = '2';
				die('notright');
			}
			else
			{
				$uid = '-' . trim($this->session->userdata('uid')) . ',';
				$uname = '-' . trim($this->session->userdata('uname')) . ',';
				
				if(strrpos($this->_fix_username, $uname, 0) === false || strrpos($this->_fix_user_id, $uid,0) === false)
				{
					$right = '2';
					die('notright');
				}
			}
		}
		
		if($right == '1')
		{
			$permitId = $this->session->userdata('permitid');
			
			$groupId = $this->mycommon->getSaveSqlStr($this->input->get('g'));
			$groupId = (is_numeric($groupId) && $groupId > -2) ? $groupId : -1;
						
			$sType = $this->mycommon->getSaveSqlStr($this->input->get('stype'));
			$sType = (is_numeric($sType) && ($sType == 1 || $sType == 2)) ? $sType : 1;
			$keyword = strip_tags(trim($this->input->get('keyword')));
			$keyword = $this->mycommon->getSaveSqlStr($keyword);
			$cPage = $this->mycommon->getSaveSqlStr($this->input->get('cpage'));
			$cPage = (is_numeric($cPage) && $cPage > 0) ? $cPage : 1;
			
			$keyword_en = $this->myvietstring->removeVietChars($keyword);
			if($sType == 2) // search by contact name
			{
				$keyword_en = '+' . $keyword_en;
				$keyword = '+' . $keyword;
			}
			else // search by username
			{
				$keyword = strtolower($this->myvietstring->removeVietChars($keyword));
				$keyword =  '%' . $keyword . '%';
			}
			
			$lstUser = $this->bigdata_model->suser_search_user($groupId, $sType, $keyword, $keyword_en, $cPage, $this->_numItem);
			
			$data['permitId'] = $permitId;
			$data['lstUser'] = $lstUser['lstUser'];
			$data['iCount'] = $lstUser['iCount'];
			$data['pCount'] = $lstUser['pCount'];
			$data['cPage'] = $cPage;
			$this->load->view($this->_template_f . 'bigdata/search_list_user_view', $data);
		}
	}
}